<?php

namespace App\Http\Controllers\Company\English;
use App\Facades\Style;
use App\Models\Common\English\EnCollArea;
use App\Models\Common\English\EnCollOption;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\ResumeFavorite;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ResumeController extends CommonController
{

    /**
     * 收到应聘简历
     * @param Request $request
     * @return mixed
     */
    public function message(Request $request){
        $user = auth()->guard('company')->user();
        //获取收到应聘简历
        $relist = DB::table('resume_send as log')
            ->join('resume as t1', 'log.resume_id', '=', 't1.id')
            ->join('company_job as t2', 'log.job_id', '=', 't2.id')
            ->select(
                'log.id','log.status','log.updated_at','t1.name','t1.sex','t1.birthday',
                't1.education','t1.homeaddress','t1.language','t2.job_title','log.resume_id','log.job_id','t2.lang_type'
            )
            ->where('log.company_uid',$user->uid)
            ->where(function ($query) use ($request) {
                if(strlen($request->keyWord)>0){
                    $query->where('t2.job_title', 'like', '%'.$request->keyWord.'%')->orWhere('t1.name','like','%'.$request->keyWord.'%');
                }
            })
            ->where(function ($query) use ($request) {
                if(strlen($request->status)>0){
                    $query->where('log.status', $request->status);
                }
            })
            ->where('log.status','!=',4)
            ->orderBy('log.created_at','DESC')
            ->paginate(20);
        //print_r($queries );

        foreach($relist as $v) {

            if ($v->lang_type == 'en') {
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = 'Man';
                        break;
                    case "2":
                        $v->cnsex = 'Woman';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = EnCollOption::getOptionName($v->education, 'education');
                $v->homeaddress = EnCollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }else{
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = '男';
                        break;
                    case "2":
                        $v->cnsex = '女';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = CollOption::getOptionName($v->education, 'education');
                $v->homeaddress = CollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }
        }
        $status=$request->status;

        return Style::view('company.english.resumeMessage',compact('relist','status'));
    }

    /**
     * 搜索应聘简历
     * @param Request $request
     * @return mixed
     */
    public function search(Request $request){
        //获取收到应聘简历
        $relist = DB::table('resume')
            ->where(function ($query) use ($request) {
                if(strlen($request->keyWord)>0){
                    $query->where('intentionjobs', 'like', '%'.$request->keyWord.'%')->orWhere('name','like','%'.$request->keyWord.'%');
                }
            })
            ->where('language','en')
            ->orderBy('updatetime','DESC')
            ->paginate(10);

        foreach($relist as $v){

            if($v->language=='en'){
                switch($v->sex){
                    case "1":
                        $v->cnsex='Man';
                        break;
                    case "2":
                        $v->cnsex='Woman';
                        break;
                    default:
                        $v->cnsex='';
                }
                $v->education=EnCollOption::getOptionName($v->education,'education');
                $v->homeaddress=EnCollArea::getAreaName($v->homeaddress);
                $v->birthday=birthday($v->birthday);
                $v->jobarea=$v->jobarea1?EnCollArea::getAreaName($v->jobarea1):'';
                $v->jobarea.=$v->jobarea2?';'.EnCollArea::getAreaName($v->jobarea2):'';
                $v->jobarea.=$v->jobarea3?';'.EnCollArea::getAreaName($v->jobarea3):'';
                $v->jobarea.=$v->jobarea4?';'.EnCollArea::getAreaName($v->jobarea4):'';
                $v->jobarea.=$v->jobarea5?';'.EnCollArea::getAreaName($v->jobarea5):'';

            }else{
                switch($v->sex){
                    case "1":
                        $v->cnsex='男';
                        break;
                    case "2":
                        $v->cnsex='女';
                        break;
                    default:
                        $v->cnsex='';
                }
                $v->education=CollOption::getOptionName($v->education,'education');
                $v->homeaddress=CollArea::getAreaName($v->homeaddress);
                $v->birthday=birthday($v->birthday);
                $v->jobarea=$v->jobarea1?CollArea::getAreaName($v->jobarea1):'';
                $v->jobarea.=$v->jobarea2?';'.CollArea::getAreaName($v->jobarea2):'';
                $v->jobarea.=$v->jobarea3?';'.CollArea::getAreaName($v->jobarea3):'';
                $v->jobarea.=$v->jobarea4?';'.CollArea::getAreaName($v->jobarea4):'';
                $v->jobarea.=$v->jobarea5?';'.CollArea::getAreaName($v->jobarea5):'';
            }

        }

        return Style::view('company.english.resumeSearch',compact('relist'));
    }

    /**
     * 面试状态设置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function status(Request $request)
    {
        $allow=array(2,3); //允许的状态码
        if(in_array($request->status,$allow)) {
            $user = auth()->guard('company')->user();
            switch($request->status){
                case "2":
                    $msg='Unsolicited';
                    break;
                case "3":
                    $msg='Declined';
                    break;
                default:'';
            }
            $input['status'] = $request->status;
            ResumeSend::where('company_uid', $user->uid)->where('id', $request->id)->update($input);
            return response()->json(['status' => $request->status, 'msg' =>$msg]);
        }
    }

    /**
     * 删除简历库
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request){
        if(is_array($request->selected_id)) {
            $user = auth()->guard('company')->user();
            $input['status'] = 4;//删除
            ResumeSend::where('company_uid', $user->uid)->whereIn('id', $request->selected_id)->update($input);
            return back()->withInput()->with('info',"Deleted!");
        }
    }

    /**
     * 放入人才库
     * @param Request $request
     */
    public function toFavorite(Request $request)
    {
        $user = auth()->guard('company')->user();
        if(is_array($request->selected_id)){
            $browseJob=ResumeSend::select('resume_id','job_id')->where('company_uid',$user->uid)->whereIn('id',$request->selected_id)->get();
            foreach($browseJob as $v){
                $favorite=ResumeFavorite::select('id')->where('uid',$user->uid)->where('status','!=',4)->where("resume_id",$v->resume_id)->first();
                if(!empty($favorite->id)){
                    $favorite->updated_at=date('Y-m-d H:i:s');
                    $favorite->status = 0;
                    $favorite->save();
                }else{
                    $favorite = new ResumeFavorite;
                    $favorite->uid = $user->uid;
                    $favorite->resume_id = $v->resume_id;
                    $favorite->job_id = $v->job_id;
                    $favorite->status = 0;
                    $favorite->save();
                }
            }
            return back()->withInput()->with('info',"Resumes have been added to the talent pool!");
        }
    }
}